import axios from "axios";
import { Notify } from "vant";
import {getToken} from "../util/auth";
const instance=axios.create({
  baseURL:"http://localhost:3009",
  timeout:5000,
});
// Add a request interceptor
instance.interceptors.request.use(function (config) {
  // Do something before request is sent
  if(getToken()){
    config.headers.authorization=`Bearer ${getToken()}`;

  }
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function (response) {
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  return response.data;

}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  const {status}=error.response;
  if(status===401){
    Notify({ type: 'warning', message: '请先进行登录' });
    window.location.href="#/login";
  }
  return Promise.reject(error);
});
export const post=(url,data)=>instance.post(url,data);
export const get=(url,params)=>instance.get(url,{params});
export const del=(url,params)=>instance.delete(url,{params});
export const put=(url,params)=>instance.put(url,params)
